//
//  LabelOrgListController.m
//  easystudy
//
//  Created by kane on 15/6/11.
//  Copyright (c) 2015年 kane. All rights reserved.
//

#import "LabelOrgListController.h"

@interface LabelOrgListController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet UILabel *titleLbl;
    __weak IBOutlet UIButton *sortButton;
    __weak IBOutlet UIButton *regionButton;
    __weak IBOutlet UITableView *dataTable;
    __weak IBOutlet UIView *optionView;
    __weak IBOutlet UITableView *optionTable;
    
    
    NSMutableArray *dataList;
    NSMutableArray *optionList;
    NSArray *sortOptionList;
    int iOptionType;
    int iSortType;
    int iRegionID;
}

@end

@implementation LabelOrgListController
@synthesize labelDict;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataList = [[NSMutableArray alloc] init];
    optionList = [[NSMutableArray alloc] init];
    sortOptionList = @[@"综合排序",@"距离优先",@"人气最高",@"评价最高"];
    iSortType = 0;
    iRegionID = 0;
    
    titleLbl.text = labelDict[@"name"];
    [self getData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)getData
{
    optionView.hidden = YES;
    
    NSString *province = @"0";
    NSString *city = @"0";
    if ([UserEntity sharedUser].AreaID) {
        //1:北京 795:上海 18:天津  2250:重庆
        int aid = [[UserEntity sharedUser].AreaID intValue];
        if (aid == 1 || aid == 795 || aid == 18 || aid == 2250) {
            province = [UserEntity sharedUser].AreaID;
        }else{
            city = [UserEntity sharedUser].AreaID;
        }
    }
    
    NSDictionary *params = @{@"lable":labelDict[@"id"],
                             @"sort":[NSNumber numberWithInt:iSortType],
                             @"city":city,
                             @"province":province,
                             @"region":[NSNumber numberWithInt:iRegionID]};
    [WebService findOrgList:params WithBlock:^(id result, NSError *error) {
        if (!error) {
            [dataList removeAllObjects];
            if ([result[@"result"] intValue] == 1) {
                [dataList addObjectsFromArray:result[@"organization"]];
            }
            [dataTable reloadData];
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败,请检查网络" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
}

#pragma mark - User Actions
- (IBAction)backBtnPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//排序
- (IBAction)sortBtnPressed:(id)sender {
    [optionList removeAllObjects];
    iOptionType = 1;
    [optionList addObjectsFromArray:sortOptionList];
    [optionTable reloadData];
    optionView.hidden = NO;
}
//区县
- (IBAction)regionBtnPressed:(id)sender {
    [optionList removeAllObjects];
    iOptionType = 2;
    
    NSArray *cityAry = [[Database sharedManager] queryCitys:[[UserEntity sharedUser].AreaID intValue]];
    NSMutableArray *tempAry = [NSMutableArray arrayWithArray:cityAry];
    [tempAry insertObject:@{@"id":@"0",@"areacode":@"",@"areaname":@"全部地区",@"parentid":@""} atIndex:0];
    [optionList addObjectsFromArray:tempAry];
    [optionTable reloadData];
    optionView.hidden = NO;
}

- (IBAction)optionCoverBtnPressed:(id)sender {
    optionView.hidden = YES;
}

#pragma mark - UITableView DataSource && Delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (tableView == optionTable) {
        return 30;
    }
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *headLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, fWidth_Current, 30)];
    headLbl.backgroundColor = ESColor;
    headLbl.textColor = [UIColor whiteColor];
    headLbl.font = [UIFont systemFontOfSize:16];
    
    if (tableView == optionTable) {
        switch (iOptionType) {
            case 1:
                headLbl.text = @"   请选择排序方式";
                break;
            case 2:
                headLbl.text = @"   请选择地区";
                break;
            default:
                break;
        }
    }

    return headLbl;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == optionTable) {
        return 44;
    }
    return 88;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == optionTable) {
        return optionList.count;
    }
    return dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == optionTable) {
        static NSString *identifier = @"OptionCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.textLabel.font = [UIFont systemFontOfSize:14.0];
        }
        switch (iOptionType) {
            case 1:{
                NSString *item = optionList[indexPath.row];
                cell.textLabel.text = item;
                cell.accessoryType = indexPath.row == iSortType? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
            }break;
            case 2:{
                NSDictionary *item = optionList[indexPath.row];
                cell.textLabel.text = item[@"areaname"];
                cell.accessoryType = [item[@"id"] intValue] == iRegionID? UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;
            }break;
                
            default:
                break;
        }
        return cell;
    }else if (tableView == dataTable){
//        static NSString *identifier = @"OrganizationCell";
//        OrganizationCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//        if (!cell) {
//            cell = [[OrganizationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        }
//        NSDictionary *item = dataList[indexPath.row];
//        
//        NSString *urlStr = [NSString stringWithFormat:@"%@/%@",BaseURL,item[@"logo_url"]];
//        
//        [cell.logoImgView setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:nil];
//        cell.orgNameLabel.text = item[@"name"];
//        cell.descLabel.text = item[@"desc"];
//        cell.addressLabel.text = [NSString stringWithFormat:@"地址：%@",item[@"address"]];
//        [cell setOrgStars:6-indexPath.row*0.5];
//        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == optionTable) {
        switch (iOptionType) {
            case 1:{
                iSortType = (int)indexPath.row;
                [sortButton setTitle:sortOptionList[iSortType] forState:UIControlStateNormal];
                [self getData];
            }break;
            case 2:{
                NSDictionary *item = optionList[indexPath.row];
                iRegionID = [item[@"id"] intValue];
                [regionButton setTitle:item[@"areaname"] forState:UIControlStateNormal];
                [self getData];
            }break;
                
            default:
                break;
        }
        
    }else if (tableView == dataTable){
//        NSDictionary *item = dataList[indexPath.row];
//        OrgDetailController *detailVC = [[OrgDetailController alloc] init];
//        detailVC.orgDict = item;
//        detailVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

@end
